package com.resataurant.Controller.LR;

import com.resataurant.Dto.AdminDto;
import com.resataurant.Dto.UserDto;
import com.resataurant.Entity.User;
import com.resataurant.Service.AdminService;
import com.resataurant.Service.Impl.AdminServiceImpl;
import com.resataurant.Service.Impl.UserServiceImpl;
import com.resataurant.Service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");//防止中文乱码

        String user=request.getParameter("account");//获取用户账号
        String pwd=request.getParameter("password");//获取用户账号
        String name=request.getParameter("nickname");//获取用户账号
        String pwd2=request.getParameter("confirmPassword");//获取用户账号

        if(user==null||user.equals("")||pwd==null||pwd.equals("")||name==null||name.equals("")||pwd2==null||pwd2.equals("")){
            request.getSession().setAttribute("sta","请填写完整信息");
            response.sendRedirect("Register.jsp");
            return;
        } else if (!pwd.equals(pwd2)){
            request.getSession().setAttribute("sta","两次密码不一致");
            response.sendRedirect("Register.jsp");
            return;
        }else {
            User user1 = new User();
            user1.setId(user);
            user1.setPassword(pwd);
            user1.setUsername(name);
            int res = userService.register(user1);
            if (res == 0) {
                //失败
                request.getSession().setAttribute("sta", "注册失败");
                //跳转回登录界面
                response.sendRedirect("Register.jsp");
            } else {
                //用户
                request.getSession().setAttribute("sta", "注册成功");
                //跳转回登录界面
                response.sendRedirect("Register.jsp");
            }
        }
    }
}
